// packageA/pages/cashService/operator/orderDetail/orderDetail.js
const { request } = require('../../../../utils/request');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    orderInfo: {
      userName: '韩梅梅',
      station: '252105-文灶加油站',
      bagNumber: '2',
      sumMoney: '12345',
      createAt: '2021-10-20 10:30:55',
      orderId: '123456'
    },
    bagsList: [],
    statusOptions: {
      '0': {
        text: '待银行接收',
        class: 'accept'
      },
      '1': {
        text: '待银行入账',
        class: 'accept'
      },
      // '2': {
      //   text: '待银行入账',
      //   class: 'accept'
      // },
      // '3': {
      //   text: '待银行入账',
      //   class: 'accept'
      // },
      '4': {
        text: '银行已入账',
        class: 'end'
      },
      // '5': {
      //   text: '待银行退回',
      //   class: 'accept'
      // },
      '6': {
        text: '银行退回',
        class: 'error'
      },
      'A': {
        text: '申请作废中',
        class: 'accept'
      },
      'B': {
        text: '申请作废成功',
        class: 'accept'
      },
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '订单详情'
    })
    // console.log(options.orderId)
    this.setData({ orderId: options.orderId })
  },
  onShow: function(){
    this.fetchOrderInfo()
  },
  /**
   * 跳转箱包编辑
   */
  toBagFill(e){
    console.log(e);
    var ind = e.currentTarget.dataset.index;
    var bagInfo  = JSON.stringify(this.data.bagsList[ind]);
    wx.navigateTo({
      url: '/pages/cashService/operator/bagFillin/bagFillin?bagInfo=' + bagInfo,
    })
  },
  deleteBag(e){
    let query={
      id:e.currentTarget.id
    }
    request('/smsk/wechat/baginfo/return/delete', 'POST', query).then(res => {
      console.log(res.data)
      if (res.data.code == 0) {
        wx.showToast({
          title: '删除成功',
          icon: 'success'
        })
        this.fetchOrderInfo()
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 获取订单详情
   */
  fetchOrderInfo() {
    request('/smsk/wechat/orderinfo/detail', 'GET', { orderId: this.data.orderId }).then(res => {
      if (res.data.code == 0) {
        let tempArr = []
        let backList = []
        let i = 0
        for (i; i < res.data.data.bagsList.length; i++) {
          if (res.data.data.bagsList[i].bagsStatus == '6') {
            backList.push(res.data.data.bagsList[i])
          } else {
            tempArr.push(res.data.data.bagsList[i])
          }
        }
        tempArr = backList.concat(tempArr)
        this.setData({
          orderInfo: res.data.data,
          bagsList: tempArr,
          ['orderInfo.stationNo']: res.data.data.bagsList[0].stationNo,
          ['orderInfo.payer']: res.data.data.bagsList[0].payer,
        })
      } else {
        console.log(res)
      }
    })
  },
  /**
   * 跳转箱包详情
   */
  fetchBagDetail(e) {
    // console.log(e.currentTarget.id)
    wx.navigateTo({
      url: '/pages/cashService/operator/bagDetail/bagDetail?bagId=' + e.currentTarget.id,
    })
  },

})